package com.example.api.service.impl;

import com.example.api.dao.CodeDao;
import com.example.api.dao.DistributionDao;
import com.example.api.dao.DriverDao;
import com.example.api.dao.TollDao;
import com.example.api.dao.VehicleDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.Code;
import com.example.api.model.entity.Distribution;
import com.example.api.model.entity.Driver;
import com.example.api.model.entity.Toll;
import com.example.api.model.entity.Vehicle;
import com.example.api.service.TimeTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TimeTaskServiceImpl implements TimeTaskService {

    private static final Logger logger = LoggerFactory.getLogger(TimeTaskServiceImpl.class);

    @Resource
    private CodeDao codeDao;

    @Resource
    private TollDao tollDao;

    @Resource
    private DistributionDao distributionDao;

    @Resource
    private DriverDao driverDao;

    @Resource
    private VehicleDao vehicleDao;

    @Override
    public void taskDeleteExpireCode() {
        List<Code> codes = codeDao.findByExpireTime();
        logger.info("待删除的逾期登录验证码数量为{}", codes.size());
        if (!codes.isEmpty()) {
            codes.forEach(code -> {
                codeDao.deleteByEmailAndValue(code.getEmail(), code.getValue());
            });
        }
    }

    @Override
    public void taskUpdateExpireToll() {
        List<Toll> tolls = tollDao.selectExpireToll();
        logger.info("待更新的逾期未支付收费信息数量为{}", tolls.size());
        if (!tolls.isEmpty()) {
            tolls.forEach(toll -> {
                Toll existToll = tollDao.selectByPrimaryKey(toll.getId());
                if (!LogisticsConstants.TOLL_PAY_0.equals(existToll.getPay())) {
                    logger.info("收费信息支付状态不正确，tollId：{}", toll.getId());
                    return;
                }
                Distribution distribution = distributionDao.selectByPrimaryKey(toll.getDid());
                if (!LogisticsConstants.DISTRIBUTION_STATUS_MINUS_1.equals(distribution.getStatus())
                        && !LogisticsConstants.DISTRIBUTION_STATUS_0.equals(distribution.getStatus())) {
                    logger.info("配送信息状态不正确，tollId：{}，distributionId：{}", toll.getId(), toll.getDid());
                    return;
                }
                Toll newToll = new Toll();
                newToll.setId(toll.getId());
                newToll.setPay(LogisticsConstants.TOLL_PAY_2);
                tollDao.updateByPrimaryKeySelective(newToll);
                Distribution newDistribution= new Distribution();
                newDistribution.setId(toll.getDid());
                newDistribution.setStatus(LogisticsConstants.DISTRIBUTION_STATUS_MINUS_1);
                distributionDao.updateByPrimaryKeySelective(newDistribution);
                Driver newDriver = new Driver();
                newDriver.setId(distribution.getDid());
                newDriver.setDriving(LogisticsConstants.DRIVER_DRIVING_1);
                newDriver.setUpdateTime(new Date());
                driverDao.updateByPrimaryKeySelective(newDriver);
                Vehicle newVehicle = new Vehicle();
                newVehicle.setId(distribution.getVid());
                newVehicle.setDriving(LogisticsConstants.VEHICLE_DRIVING_1);
                vehicleDao.updateByPrimaryKeySelective(newVehicle);
            });
        }
    }

}
